package com.whfc.ms.api.mach.controller.gscx;

import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.SysMachTypeAddParam;
import com.whfc.fuum.param.SysMachTypeClassifyAddParam;
import com.whfc.fuum.param.SysMachTypeClassifyEditParam;
import com.whfc.fuum.param.SysMachTypeEditParam;
import com.whfc.ms.api.mach.service.MsMachTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClasssName MsMachTypeController
 * @Description 系统管理-设备类型
 * @Author hw
 * @Date 2020/8/24 20:23
 * @Version 1.0
 */
@RestController
@RequestMapping("/ms/api/gscx/sys/mach/type")
public class MsGscxMachTypeController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsMachTypeService msMachTypeService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam(value = "machTypeId") Integer machTypeId,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "pageNum") Integer pageNum,
                       @RequestParam(value = "pageSize") Integer pageSize,
                       @RequestParam(value = "state", required = false) Integer state) {
        logger.info("查询设备类型。loginer:{},machTypeId:{},keyword:{},pageNum:{},pageSize:{}", user.getUsername(), machTypeId,
                keyword, pageNum, pageNum);
        return ResultUtil.success(msMachTypeService.machTypeList(machTypeId, pageNum, pageSize, keyword,state));
    }

    @GetMapping("/classify/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("查询设备分类。loginer:{},keyword:{}", user.getUsername(), keyword);
        return ResultUtil.success(msMachTypeService.machTypeClassifyList(keyword));
    }

    @RequiresPermissions(value = {"sys/machType"})
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated SysMachTypeAddParam param) {
        logger.info("新增设备类型。loginer:{},param:{}", user.getUsername(), param);
        msMachTypeService.machTypeAdd(param);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/machType"})
    @PostMapping("/classify/add")
    public Result classifyAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated SysMachTypeClassifyAddParam param) {
        logger.info("新增设备分类。loginer:{},param:{}", user.getUsername(), param);
        msMachTypeService.machTypeClassifyAdd(param);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/machType"})
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated SysMachTypeEditParam param) {
        logger.info("新增设备类型。loginer:{},param:{}", user.getUsername(), param);
        msMachTypeService.machTypeEdit(param);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/machType"})
    @PostMapping("/classify/edit")
    public Result classifyEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated SysMachTypeClassifyEditParam param) {
        logger.info("新增设备类型。loginer:{},param:{}", user.getUsername(), param);
        msMachTypeService.machTypeClassifyEdit(param);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/machType"})
    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("id") Integer id) {
        logger.info("删除设备类型。loginer:{},machTypeId:{}", user.getUsername(), id);
        msMachTypeService.machTypeDel(id);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/machType"})
    @GetMapping("/enable")
    public Result enable(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam(value = "machTypeId") Integer machTypeId,
                         @RequestParam(value = "state") Integer state) {
        logger.info("查询设备分类。loginer:{},machTypeId:{},state:{}", user.getUsername(), machTypeId, state);
        msMachTypeService.enable(machTypeId, state);
        return ResultUtil.success();
    }

    @GetMapping("/tree")
    public Result tree(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam(value = "machTypeId",  required = false) Integer machTypeId){
        logger.info("查询设备类型树。loginer:{}", user.getUsername());
        return ResultUtil.success(msMachTypeService.machLists(machTypeId));
    }

}
